<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item><i class="el-icon-user"></i> 用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="query.name" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="getUsers"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4" v-has-permission="'sys:user:add'">
          <el-button type="primary" @click="addOpenDialog">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table
        :data="users"
        style="width: 100%">
        <el-table-column
          prop="username"
          label="登录名"
          width="180">
        </el-table-column>
        <el-table-column
          prop="cname"
          label="用户名"
          width="180">
        </el-table-column>
        <el-table-column
          prop="isstop"
          label="是否停用">
          <el-switch
                  style="display: block"
                  v-model="users.isdel"
                  active-color="#ff4949"
                  inactive-color="#13ce66"
                  active-text="已停用"
                  inactive-text="未停用"
                  disabled
          >
          </el-switch>
        </el-table-column>
        <el-table-column
          prop="roles"
          label="拥有角色">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="180">
          <template slot-scope="scope">
            <el-button @click="editOpen(scope.row)" type="text" size="small">修改</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteUser(scope.row)">删除</el-button>
            <el-button type="text" size="small" @click="setRole(scope.row)">设置</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[5, 10, 15, 30]"
        :page-size="query.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <el-dialog :close-on-click-modal="false"
      title="新增用户"
      :visible.sync="addDialogVisible"
      width="50%">
      <el-form ref="form" :model="user" label-width="80px">
        <el-form-item label="登录名">
          <el-input v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="user.cname"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-switch
            v-model="user.gender"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value=true
            inactive-value=false>
          </el-switch>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="user.phone"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="user.remarks"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog :close-on-click-modal="false"
               title="修改用户"
               :visible.sync="editDialogVisible"
               width="50%">
      <el-form ref="editForm" :model="user" label-width="80px">
        <el-form-item label="登录名" >
          <el-input v-model="user.username" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="user.cname"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-switch
            v-model="user.gender"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value=true
            inactive-value=false>
          </el-switch>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="user.phone"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="user.remarks"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog :close-on-click-modal="false"
               title="设置用户角色"
               :visible.sync="setDialogVisible"
               width="50%">
      <el-transfer
        :props="{
          key: 'id',
          label: 'cname'
        }"
        v-model="hasRoles"
        :titles="['未拥有', '已拥有']"
        :data="roles">
      </el-transfer>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRoles">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {getData,putData,postData,deleteData} from '../api/index'
  export default {
    name: "User",
    mounted() {
      this.getUsers()
      this.getAllRoles()
    },
    methods: {
      async setRoles() {
        const userId = this.userid
        const roleIdsAndUserIDs = this.hasRoles.map(roleId => {
          return {
            userid: userId,
            roleid: roleId
          }
        })
        const hasRoles = this.hasRoles
        const roleNames = this.roles.filter(r => {
          return hasRoles.indexOf(r.id) !== -1
        }).map(r => {
          return r.cname
        }).join(",") || "-1"

        const res=await postData("/sys/userRole/userid/" + userId + "/roleNames/" + roleNames, roleIdsAndUserIDs)
        if(res.code===20000){
          this.setDialogVisible =false
          this.$message.success(res.msg)
        }else {
          this.$message.error(res.msg)
        }
      },
      async getAllRoles() {
        const res = await getData("/sys/role/all")
        if(res.code ===20000){
          this.roles =res.data
        }
      },
      async edit() {
        const res = await putData("/sys/user/" + this.user.id, this.user)
        if(res.code === 20000){
          this.editDialogVisible = false
          this.$message.success(res.msg)
          await this.getUsers()
        }else {
          this.$message.error(res.msg)
        }
      },
      editOpen(userInfo){
        this.editUser = userInfo
        this.user = JSON.parse(JSON.stringify(userInfo))
        this.editDialogVisible =true
      },
      deleteUser(userInfo){
        this.$confirm('您真的要删除此纪录吗?', '删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async  () => {
          const res = await deleteData("/sys/user/"+userInfo.id)
          if(res.code === 20000){
            this.$message.success(res.msg)
            await  this.getUsers()
          }else{
            this.$message.error(res.msg)
          }
        }).catch(() => {

        });
      },
      async setRole(userInfo) {
        this.setDialogVisible = true
        this.userid= userInfo.id
        const res =await getData("/sys/user/userid/" + userInfo.id)
        if(res.code === 20000){
          this.hasRoles =res.data
        }
      },
      addOpenDialog(){
        this.user ={}
        this.addDialogVisible = true
      },
      async add() {
        const res = await postData('/sys/user', this.user)
        if(res.code===20000){
          this.addDialogVisible = false
          await this.getUsers()
          this.$message.success(res.msg)
        }else{
          this.$message.error(res.msg)
        }
      },
      async getUsers() {
        const res = await getData('/sys/user', this.query)
        if(res.code===20000){
          this.users = res.data.data
          this.total = res.data.total
        }
      },
      handleSizeChange(val) {
        this.query.size = val
        this.getUsers()
      },
      handleCurrentChange(val) {
        this.query.page = val
        this.getUsers()
      }
    },
    data(){

      return {
        value:false,
        userid: -1,
        roles: [],
        hasRoles: [1,2],
        setDialogVisible: false,
        editDialogVisible: false,
        user:{},
        addDialogVisible: false,
        users: [],
        total: 0,
        query: {
          name: '',
          page: 1,
          size: 5
        }
      }
    }
  }
</script>

<style scoped>

</style>
